﻿#include "GUIGeometryPlaneDialog.h"
#include "ui_GUIGeometryPlaneDialog.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelOperLine.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeomTools.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelSurface.h"
#include "PickDataProvider/GUIPickInfo.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/PickedDataProvider.h"

#include <QMessageBox>
namespace GUI {
    GUIGeometryPlaneDialog::GUIGeometryPlaneDialog(Core::FITKActionOperator* oper, QWidget* parent)
        : GUIGeometryBaseDialog(nullptr, oper, parent), _ui(new Ui::GUIGeometryPlaneDialog)
    {
        _ui->setupUi(this);
        this->createGeometryObj(Interface::FITKGeoEnum::FITKGeometryComType::FGTClosedWireSurface);//封闭曲线形成的面
        this->init();
    }

    GUIGeometryPlaneDialog::GUIGeometryPlaneDialog(Interface::FITKAbsGeoModelSurface* obj, Core::FITKActionOperator* oper, QWidget* parent)
        : GUIGeometryBaseDialog(obj, oper, parent), _ui(new Ui::GUIGeometryPlaneDialog)
    {
        _ui->setupUi(this);

        this->init();
    }

    GUIGeometryPlaneDialog::~GUIGeometryPlaneDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }
    void GUIGeometryPlaneDialog::init()
    {

        setWindowTitle(tr("Surface"));

        Interface::FITKAbsGeoModelClosedWireSurface* geoData = dynamic_cast<Interface::FITKAbsGeoModelClosedWireSurface*>(_obj);
        Acoustics::FITKAcousticsGeometryData* geometryData = FITKAPP->getGlobalData()->getGeometryData<Acoustics::FITKAcousticsGeometryData>();

        if (!geometryData || !geoData) return;
        //获取数据
        QString name = geometryData->checkName(tr("Surface-1"));
        if (!_isCreate) name = _obj->getDataObjectName();

        _ui->lineEdit_Name->setText(name);
    }
    void GUIGeometryPlaneDialog::pickFinishedOper()
    {
        //获取拾取数据
        GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        if (provider == nullptr)return;
        disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(slotPickOverEvent()));
        QList<GraphData::PickedData*> pickList = provider->getPickedList();//拾取到那些直线的IDlist
        QList<Interface::VirtualShape> edges;

        for (GraphData::PickedData* pickData : pickList) {
            if (pickData == nullptr)continue;
            int cmdID = pickData->getPickedDataObjId();
            QHash<int, int>OtherIDs = pickData->getPickedOtherIDs();//key PickedId  value index
            for (int i : pickData->getPickedIds())
            {
                if (OtherIDs.contains(i) == true)
                {
                    Interface::VirtualShape temp(cmdID, i, OtherIDs.value(i));
                    edges.push_back(temp);
                }
            }
        }

        Interface::FITKAbsGeoModelClosedWireSurface*PlaneP = dynamic_cast<Interface::FITKAbsGeoModelClosedWireSurface*>(_obj);
        if (PlaneP == nullptr)
        {
            return;
        }
        PlaneP->setEdges(edges);
    }
    void GUIGeometryPlaneDialog::OnpickInProcess()
    {
        //转换为string
        QString number="("+QString::number(m_pick_num)+")";
        _ui->Pick_num->setText(number);
    }
    void GUIGeometryPlaneDialog::getDataFormWidget()
    {
        Interface::FITKAbsGeoModelClosedWireSurface*PlaneP = dynamic_cast<Interface::FITKAbsGeoModelClosedWireSurface*>(_obj);
        PlaneP->setDataObjectName(_ui->lineEdit_Name->text());//更新面名称
    }

 
    void GUIGeometryPlaneDialog::on_pushButton_OK_clicked()
    {
        this->pickFinishedOper();//调用获取数据

        this->check();

        if (_isCreate)
        {
            if (!this->createObjData())
            {
                return;
            }
        }
        else
        {
            if (!this->editObjData())
                return;
        }
        if (_oper && _obj) {
            _oper->setArgs("objID", _obj->getDataObjectID());
        }
        this->accept();
    }

    void GUIGeometryPlaneDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }
    void GUIGeometryPlaneDialog::on_pushButton_Pick_clicked()
    {
        //几何线 点选+框选+追加拾取
        enablePickSig(GUI::GUIPickInfo::POBJGeometryEdge, GUI::GUIPickInfo::PickMethod::PMIndividually);
    }
    bool GUIGeometryPlaneDialog::check()
    {
        _oper->setArgs("check_state", true);//默认设置为正确true
        Interface::FITKAbsGeoModelClosedWireSurface*PlaneP = dynamic_cast<Interface::FITKAbsGeoModelClosedWireSurface*>(_obj);
        if (PlaneP == nullptr)
        {
            return false;
        }

        //检查是否为大于等于3个的线段,构成面的最小条件就是起码3个线
        QList<Interface::VirtualShape>data = PlaneP->edges();
        if (data.size() < 3)
        {
            QMessageBox msgBox;
            msgBox.setIcon(QMessageBox::Warning);  // 设置消息框图标为警告图标
            msgBox.setWindowTitle(tr("warning"));
            msgBox.setText(tr("At least 3 straight lines need to be selected"));//最少需要3条直线
            msgBox.setStandardButtons(QMessageBox::Ok);  // 设置标准按钮
            msgBox.setDefaultButton(QMessageBox::Ok);  // 设置默认按钮
            int ret = msgBox.exec();//事件循环

            _oper->setArgs("check_state",false );
            return false;
        }

        return true;
    }
}